package com.ztools.mvp;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.ztools.base.BaseDialogFragment;
import com.ztools.mvp.proxy.MvpBindCallback;
import com.ztools.mvp.proxy.impl.FragmentLifecycleCallbackProxy;


/**
 * <pre>
 *    author: summer
 *    time  : 2018/03/06
 * </pre>
 */

public abstract class BaseMvpDialogFragment<V extends IView, P extends IPresenter<V>> extends BaseDialogFragment implements MvpBindCallback<V, P> {

    private P p;
    private V v;

    //获取目标接口的引用
    private FragmentLifecycleCallbackProxy<V, P> mLifeCallbackProxy;

    public FragmentLifecycleCallbackProxy<V, P> getLifeCallbackProxy() {
        if (this.mLifeCallbackProxy == null) {
            this.mLifeCallbackProxy = new FragmentLifecycleCallbackProxy<>(this);
        }
        return this.mLifeCallbackProxy;
    }

    @Override
    public P newPresenter() {
        return this.p;
    }

    @Override
    public V newMvpView() {
        return this.v;
    }

    @Override
    public P getPresenter() {
        return this.p;
    }

    @Override
    public void setPresenter(P p) {
        this.p = p;
    }

    @Override
    public V getMvpView() {
        return this.v;
    }

    @Override
    public void setMvpView(V v) {
        this.v = v;
    }

    @Override
    public void onAttach(Context context) {
        getLifeCallbackProxy().onAttach();
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifeCallbackProxy().onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getLifeCallbackProxy().onActivityCreated(savedInstanceState);
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getLifeCallbackProxy().onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        getLifeCallbackProxy().onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
        getLifeCallbackProxy().onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        getLifeCallbackProxy().onResume();
    }

    @Override
    public void onStop() {
        super.onStop();
        getLifeCallbackProxy().onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getLifeCallbackProxy().onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getLifeCallbackProxy().onDestroy();
    }
}
